package cn.iocoder.yudao.module.nginxui.controller.admin.globalblock;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.nginxui.controller.admin.globalblock.vo.*;
import cn.iocoder.yudao.module.nginxui.dal.dataobject.globalblock.GlobalBlockDO;
import cn.iocoder.yudao.module.nginxui.service.globalblock.GlobalBlockService;

@Tag(name = "管理后台 - Nginx全局块配置")
@RestController
@RequestMapping("/nginxui/global-block")
@Validated
public class GlobalBlockController {

    @Resource
    private GlobalBlockService globalBlockService;

    @PostMapping("/create")
    @Operation(summary = "创建Nginx全局块配置")
    @PreAuthorize("@ss.hasPermission('nginxui:global-block:create')")
    public CommonResult<Long> createGlobalBlock(@Valid @RequestBody GlobalBlockSaveReqVO createReqVO) {
        return success(globalBlockService.createGlobalBlock(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新Nginx全局块配置")
    @PreAuthorize("@ss.hasPermission('nginxui:global-block:update')")
    public CommonResult<Boolean> updateGlobalBlock(@Valid @RequestBody GlobalBlockSaveReqVO updateReqVO) {
        globalBlockService.updateGlobalBlock(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除Nginx全局块配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('nginxui:global-block:delete')")
    public CommonResult<Boolean> deleteGlobalBlock(@RequestParam("id") Long id) {
        globalBlockService.deleteGlobalBlock(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除Nginx全局块配置")
                @PreAuthorize("@ss.hasPermission('nginxui:global-block:delete')")
    public CommonResult<Boolean> deleteGlobalBlockList(@RequestParam("ids") List<Long> ids) {
        globalBlockService.deleteGlobalBlockListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得Nginx全局块配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('nginxui:global-block:query')")
    public CommonResult<GlobalBlockRespVO> getGlobalBlock(@RequestParam("id") Long id) {
        GlobalBlockDO globalBlock = globalBlockService.getGlobalBlock(id);
        return success(BeanUtils.toBean(globalBlock, GlobalBlockRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得Nginx全局块配置分页")
    @PreAuthorize("@ss.hasPermission('nginxui:global-block:query')")
    public CommonResult<PageResult<GlobalBlockRespVO>> getGlobalBlockPage(@Valid GlobalBlockPageReqVO pageReqVO) {
        PageResult<GlobalBlockDO> pageResult = globalBlockService.getGlobalBlockPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GlobalBlockRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出Nginx全局块配置 Excel")
    @PreAuthorize("@ss.hasPermission('nginxui:global-block:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportGlobalBlockExcel(@Valid GlobalBlockPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GlobalBlockDO> list = globalBlockService.getGlobalBlockPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "Nginx全局块配置.xls", "数据", GlobalBlockRespVO.class,
                        BeanUtils.toBean(list, GlobalBlockRespVO.class));
    }

}